#!/bin/bash
# vim: set ft=sh

set -eu
set -o pipefail

exec 3>&1 # make stdout available as fd 3 for the result
exec 1>&2 # redirect all output to stderr for logging

resource_dir=$(dirname $0)

source $resource_dir/common.sh

destination=${1:-}
if [ -z "$destination" ]; then
  echo "usage: $0 <path/to/destination>"
  exit 1
fi

# for jq
PATH=/usr/local/bin:$PATH

TMPDIR=${TMPDIR:-/tmp}
payload=$(mktemp $TMPDIR/maven-resource-request.XXXXXX)
working=$TMPDIR/maven-resource-working && mkdir -p $working

cat > $payload <&0

debug=$(jq -r '.source.debug //empty' < $payload)
if [ "$debug" = "true" ]; then
  set -x
fi

release_url=$(jq -r '.source.url //empty' < $payload)
snapshot_url=$(jq -r '.source.snapshot_url //empty' < $payload)
artifact=$(jq -r '.source.artifact //empty' < $payload)
version=$(jq -r '.version.version //empty' < $payload)
username=$(jq -r '.source.username //empty' < $payload)
password=$(jq -r '.source.password //empty' < $payload)
skip_cert_check=$(jq -r '.source.skip_cert_check //empty' < $payload)
repository_cert=$(jq -r '.source.repository_cert //empty' < $payload)

# TODO: Add more error checking
if [ -z "$release_url" ] && [ -z "$snapshot_url" ] ; then
  echo "[ERROR] invalid payload (must specify url or snapshot_url)"
  exit 1
fi

if [ -z "$artifact" ]; then
  echo "[ERROR] invalid payload (missing artifact)"
  exit 1
fi

sourceMavenOpts=$(jq -r '.source.maven_opts //empty' < $payload)
sourceMavenConfig=$(jq -r '.source.maven_config //empty' < $payload)
paramsMavenOpts=$(jq -r '.params.maven_opts //empty' < $payload)
paramsMavenConfig=$(jq -r '.params.maven_config //empty' < $payload)

# Allow overriding the source config values
export MAVEN_OPTS=${paramsMavenOpts:-$sourceMavenOpts}
export MAVEN_CONFIG=${paramsMavenConfig:-$sourceMavenConfig}
export MAVEN_BASEDIR=$resource_dir

# Use ours if user didn't specify
[ -z "$MAVEN_CONFIG" ] && MAVEN_CONFIG="-s $resource_dir/.mvn/settings.xml"

# groupId:artifactId:type[:classifier]
groupId=$(get_group_id $artifact)
artifactId=$(get_artifact_id $artifact)
packaging=$(get_packaging $artifact)
classifier=$(get_classifier $artifact)

artifactItem="$groupId:$artifactId:$version:$packaging"
[ -n "$classifier" ] && artifactItem="$artifactItem:$classifier"

if [ "$skip_cert_check" = "true" ]; then
  MAVEN_OPTS="$MAVEN_OPTS -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true"
elif [ -n "$repository_cert" ]; then
  mkdir -p $working/security
  echo "$repository_cert" > $working/security/repository.crt
  : "${JAVA_HOME:?[ERROR] JAVA_HOME not set or empty}"
  cp  $JAVA_HOME/jre/lib/security/cacerts $working/security/.
  keytool -noprompt -storepass changeit -keystore $working/security/cacerts -import -file $working/security/repository.crt -alias maven-resource-repository-CAAlias
  MAVEN_OPTS="$MAVEN_OPTS -Djavax.net.ssl.trustStore=$working/security/cacerts -Djavax.net.ssl.trustStorePassword=changeit"
fi

# test for 1.0.0-SNAPSHOT and for 1.0.0-20170328.031519-19
isSnapshot=false
[[ "$version" = *-SNAPSHOT ]] || echo "$version" | grep -oEq "[0-9]{8}\.[0-9]{6}-[0-9]{1,}" && isSnapshot=true

url=$release_url
[ -n "$snapshot_url" ] && [ "$isSnapshot" = true ] && url=$snapshot_url

args=
args="$args -Dartifact=$artifactItem"
args="$args -DoutputDirectory=$destination"
args="$args -Drepository.url=$url"

[ -n "$username" ] && args="$args -Drepository.username=$username";
[ -n "$password" ] && args="$args -Drepository.password=$password";

$resource_dir/mvnw dependency:copy $args

jq -n \
--arg version "$version" \
'{
  version: {version: $version}
}' >&3
